package service;

import java.sql.SQLException;
import java.util.List;

import dao.BookDao;
import dao.OrderDao;
import dao.OrderItemDao;
import entity.Order;
import entity.OrderItem;
import entity.User;
import util.DbUtil;
public class OrderService {
	private OrderDao odao=new OrderDao();
	private OrderItemDao oidao=new OrderItemDao();
	private BookDao bdao=new BookDao();		
	public void addOrder(Order order)  {
		//添加订单和订单的条目
		try {
			DbUtil.startTransaction();
			odao.addOrder(order);
			oidao.addOrderItems(order.getOrderItems());
			//修改商品的数量
			bdao.changProductNum(order);
			DbUtil.releaseAndCloseConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				DbUtil.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
		  
	}

	/*
	 * 查询后台所有的订单
	 */
	public List<Order> findAllOrder() {
		// TODO Auto-generated method stub
		OrderDao dao=new OrderDao();
		try {
			
			List<Order> list = dao.findAllOrder();
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/*
	 * 查询后台有条件的订单
	 */
	public List<Order> findOrderByManyCondition(String id, String receiverName) {
		// TODO Auto-generated method stub
		OrderDao dao=new OrderDao();
		try {
			
			List<Order> list = dao.findOrderByManyCondition(id,receiverName);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	// 根据id查找订单
	public Order findOrderById(String id) {
		// TODO Auto-generated method stub
		Order order = null;
		try {
			order = odao.findOrderById(id);
			List<OrderItem> items = oidao.findOrderItemByOrder(order);
			order.setOrderItems(items);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return order;
		
	}
	//根据用户查找订单
		public List<Order> findOrderByUser(User user) {
			// TODO Auto-generated method stub
			List<Order> orders = null;
			try {
				// 查找出订单信息
				orders = odao.findOrderByUser(user);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return orders;
		}
		//普通用户删除订单
		public void delOrderByIdWithClient(String id) {
			try {
				DbUtil.startTransaction();//开启事务
				//从订单项中查找商品购买数量
				Order order=new Order();
				order.setId(id);
				List<OrderItem> items=oidao.findOrderItemByOrder(order);
				//修改商品数量			
				bdao.updateProductNum(items);						
				oidao.delOrderItems(id); //删除订单项
				odao.delOrderById(id); //删除订单
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					DbUtil.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}finally{
				try {
					DbUtil.releaseAndCloseConnection();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		//根据id删除订单 管理员删除订单
		public void delOrderById(String id) {			
			try {
				DbUtil.startTransaction();//开启事务
				oidao.delOrderItems(id);
				odao.delOrderById(id);
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					DbUtil.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				} 
			}finally{
				try {
					DbUtil.releaseAndCloseConnection();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}		
		}
		

	


}
